package com.inspur.edp.ai.flow.node;

import com.inspur.edp.ai.flow.engine.FlowContext;
import com.inspur.edp.ai.flow.engine.strategy.DefaultFlowStrategy;
import com.inspur.edp.ai.flow.metadata.AbsNode;
import com.inspur.edp.ai.flow.metadata.Parameter;
import com.inspur.edp.ai.flow.spi.NodeExecuteResult;
import com.inspur.edp.ai.flow.spi.NodeExecutor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lizhaorui
 * @date 2025/9/16
 * @description
 */

public class VariableDefNodeExecutor implements NodeExecutor {


    @Override
    public String getKind() {
        return VariableDefNode.KIND;
    }

    @Override
    public NodeExecuteResult execute(FlowContext context, AbsNode node, Map<String, Object> args) {
        VariableDefNode varibleNode = (VariableDefNode) node;
        Map<String, Object> outputMap = new HashMap<>();
        if (varibleNode.getOutputParams() != null) {
            for (Parameter parameter : varibleNode.getOutputParams()) {
                if (parameter.getValueExprEvaluator() != null) {
                    Object paramValue = parameter.getValueExprEvaluator().evaluate(args);
                    outputMap.put(parameter.getCode(), paramValue);
                } else {
                    outputMap.put(parameter.getCode(), null);
                }
            }
        }
        NodeExecuteResult nodeExecuteResult = new NodeExecuteResult();
        nodeExecuteResult.setVariablesToAdd(outputMap);
        DefaultFlowStrategy strategy = new DefaultFlowStrategy(node.getId(), node.getOutputPorts().get(0));
        nodeExecuteResult.setFlowStrategy(strategy);
        nodeExecuteResult.setReturn(false);
        return nodeExecuteResult;
    }
}